package com.ruoyi.file.component;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.file.controller.FileLocalChunkController;
import com.ruoyi.file.domain.*;
import com.ruoyi.file.service.IFileChunkInfoService;
import com.ruoyi.file.service.IFileInfoService;
import com.ruoyi.file.service.ITblFileService;
import com.ruoyi.framework.minio.MinioFile;
import com.ruoyi.framework.minio.MinioService;
import com.ruoyi.gis.domain.GisLayer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PipedReader;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class FileLocalComponent {
    private final Logger logger = LoggerFactory.getLogger(FileLocalComponent.class);

    @Autowired
    private MinioService minioService;
    @Autowired
    private ITblFileService tblFileService;
    @Autowired
    private PublishLayerProcess publishLayerProcess;
    @Autowired
    private RuoYiConfig ruoYiConfig;


//    public void processUploadFile(LoginUser loginUser, MinioFile minioFile, TFileInfoLocalVO chunk) throws FileNotFoundException {
//        File bucketFolder = new File(ruoYiConfig.getMinioLocalDir(),minioFile.getBucket());
//        File objectFile =  new File(bucketFolder,minioFile.getObjectName());
//        //保存到数据库
//        TblFile tblFile = saveIntoDb(loginUser, minioFile, chunk);
//        //保存到minio
//        MinioFile minioFileUpload = minioService.uploadFile(new FileInputStream(objectFile), minioFile.getBucket(), minioFile.getObjectName());
//        if(minioFileUpload==null){
//            logger.error("processUploadFile","upload to minio error");
//            return;
//        }
//        //发布
//        if(tblFile.getPublishable()!=null&&tblFile.getPublishable()){
//            GisLayer gisLayerGroup = null;
//            if(tblFile.getId()!=0){
//                gisLayerGroup = tblFileService.addParentLayer(tblFile,loginUser);
//            }
////            publishFilesMsg.add(publishDto);
//            if(FileUtils.isTifFile(chunk.getName())){
//                FilePublishDto publishDto = new FilePublishDto();
//                publishDto.setPublishFileId(tblFile.getId());
//                publishDto.setParentLayerId(gisLayerGroup!=null?gisLayerGroup.getLayerId():0L);
//                publishLayerProcess.sendPublishLayerMessage(publishDto);
//            }else if(FileUtils.isShpFile(chunk.getName())){
//                //等待发布
//                FilePublishDelayDto publishDto = new FilePublishDelayDto();
//                publishDto.setPublishFileId(tblFile.getId());
//                publishDto.setParentLayerId(gisLayerGroup!=null?gisLayerGroup.getLayerId():0L);
//                String replace = chunk.getRelativePath().replace(".shp", "");
//                List<TFileInfoVO> files = chunk.getRootFile().getFiles().stream().filter(item->item.getRelativePath().contains(replace)).collect(Collectors.toList());
//                publishDto.setShpFilesCount(files.size());
//                publishLayerProcess.sendDelayPublisyLayerMessage(publishDto);
//            }
//        }
//    }

//    private TblFile saveIntoDb(LoginUser loginUser, MinioFile minioFile, TFileInfoLocalVO chunk) {
//        TblFile parent = null;
//        if(0==chunk.getPid()){
//            parent = new TblFile();
//            parent.setId(0l);
//        }else {
//            parent = tblFileService.selectTblFileById(chunk.getPid());
//        }
//        Long userId = loginUser.getUserId();
//        if(parent!=null){
//            List<String> fDirs = new ArrayList<>();
//            if(0==chunk.getPid()){
//                fDirs.add(userId+"");
//            }
//            String[] folders = chunk.getRelativePath().split("/");//File.separator
//            //最后一个为文件
//            for(int i=0;i<folders.length-1;i++){
//                fDirs.add(folders[i]);
//                TblFile fileFolder = tblFileService.addFolder(
//                        userId,parent.getId(), folders[i]);
//                if(fileFolder!=null){
//                    parent = fileFolder;
//                }
//            }
//            String folder = StringUtils.join(fDirs,"/");
//            if(0==parent.getId()){
//            }else {
//                folder = parent.getFilePath();
//            }
//            TblFile entity = new TblFile();
//            entity.setId(IdUtil.getSnowflakeNextId());
//            entity.setPid(parent.getId());
//            entity.setFileName(chunk.getName());
//            entity.setFileSize(chunk.getSize());
//            entity.setUserId(userId);
//            entity.setIdentifier(chunk.getUniqueIdentifier());
//            String ext = FileUtils.getExt(chunk.getName());
//            entity.setContentType(ext);
//            if(FileUtils.isTifFile(chunk.getName())){
//                entity.setPublishable(true);
//            }else if(FileUtils.isShpFile(chunk.getName())){
//                entity.setPublishable(true);
//            }
//            entity.setFileType(TblFile.type_file);
//            entity.setBucket(minioFile.getBucket());
//            entity.setFilePath(folder);
//            entity.setObjectName(minioFile.getObjectName());
//            tblFileService.insertTblFile(entity);
//            //更新上级目录大小
//            tblFileService.updateFolderSize(entity.getPid());
////                if(0!=parent.getId()){
////                    long folderSize = tblFileService.sumFolderSize(parent.getId());
////                    parent.setFileSize(folderSize);
////                    tblFileService.updateTblFile(parent);
////                }
//            return entity;
//        }
//        return null;
//    }
}
